

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class ScannerGenerator {
	
	final int BEGIN = 144;
	final int END = 145;
	final int POUND = 146;
	final int MAXFREQSTRING = 147;
	final int ID = 148;
	final int EQUAL = 149;
	final int SEMICOLON = 150;
	final int REPLACE = 151;
	final int REGEX = 152;
	final int WITH = 153;
	final int ASCIISTR = 154;
	final int WORD_IN = 155;
	final int RECURSIVEREPLACE = 156;
	final int PRINT = 157;
	final int GREATEREXCLAM = 158;
	final int COMMA = 159;
	final int FIND = 160;
	final int DIFF = 161;
	final int UNION = 162;
	final int INTERS = 163;
	final int LEFT_PAREN = 164;
	final int RIGHT_PAREN = 165;
	boolean done;
	ArrayList<Token> tokenList = new ArrayList<Token>();
	
	public ArrayList<Token> generateList(String file){
		Token newtoken = null;
		
		try {
			FileInputStream fileStream = new FileInputStream(file);
			DataInputStream dataStream = new DataInputStream(fileStream);
			BufferedReader br = new BufferedReader(new InputStreamReader(dataStream));
			String fileLine, keyword;
			int pos;
			while ((fileLine = br.readLine()) != null) {
				pos = 0;
				keyword = "";
				done = false;
				while(!done){
					//Get rid of white space
					if(fileLine.charAt(pos) == ' '){
						if(pos < fileLine.length()-1){
							pos++;
						}
						else{
							done = true;
						}
					}
					//Check if it is a regex
					if(fileLine.charAt(pos) == '\'' && keyword.equals("")){
						keyword += fileLine.charAt(pos);
						pos++;
						while(fileLine.charAt(pos) !=  '\'' && fileLine.charAt(pos-1) != '\\'){
							keyword += fileLine.charAt(pos);
							pos++;
						}
					}
					else{
						if(fileLine.charAt(pos) != ' '){
							if(pos < fileLine.length()){
								keyword += fileLine.charAt(pos);
								if(pos < fileLine.length()-1){
									pos++;
								}
								else{
									done = true;
								}
							}
							else{
								done = true;
							}
						}	
							
					}
					
					switch(keyword){
					case "begin":
						newtoken = new Token(BEGIN,keyword);
						keyword = "";
						break;
					case "end":
						newtoken = new Token(END,keyword);
						keyword = "";
						break;
					case "=":
						newtoken = new Token(EQUAL,keyword);
						keyword = "";
						break;
					case "replace":
						newtoken = new Token(REPLACE,keyword);
						keyword = "";
						break;
					case "with":
						newtoken = new Token(WITH,keyword);
						keyword = "";
						break;
					case "in":
						if(fileLine.charAt(pos) == ' '){
							newtoken = new Token(WORD_IN,keyword);
							keyword = "";
						}
						break;
					case ";":
						newtoken = new Token(SEMICOLON,keyword);
						keyword = "";
						break;
					case "recursivereplace":
						newtoken = new Token(RECURSIVEREPLACE,keyword);
						keyword = "";
						break;
					case ">!":
						newtoken = new Token(GREATEREXCLAM,keyword);
						keyword = "";
						break;
					case "print":
						newtoken = new Token(PRINT,keyword);
						keyword = "";
						break;
					case "(":
						newtoken = new Token(LEFT_PAREN,keyword);
						keyword = "";
						break;
					case ")":
						newtoken = new Token(RIGHT_PAREN,keyword);
						keyword = "";
						break;
					case ",":
						newtoken = new Token(COMMA,keyword);
						keyword = "";
						break;
					case "#":
						newtoken = new Token(POUND,keyword);
						keyword = "";
						break;
					case "find":
						newtoken = new Token(FIND,keyword);
						keyword = "";
						break;
					case "diff":
						newtoken = new Token(DIFF,keyword);
						keyword = "";
						break;
					case "union":
						newtoken = new Token(UNION,keyword);
						keyword = "";
						break;
					case "inters":
						newtoken = new Token(INTERS,keyword);
						keyword = "";
						break;
					case "maxfreqstring":
						newtoken = new Token(MAXFREQSTRING,keyword);
						keyword = "";
						break;
				}
				//See if keyword is ID or ASCIISTR or REGEX
				if(!keyword.equals("")){
					//REGEX
					if(keyword.charAt(0) == '\'' && keyword.charAt(keyword.length()-1) == '\'' && keyword.length() > 1){
						newtoken = new Token(REGEX,keyword);
						newtoken.setRegex(keyword.substring(1, keyword.length()-1));
						keyword = "";
					}
					//ASCIISTR
					else if(keyword.charAt(0) == '"' && keyword.charAt(keyword.length()-1) == '"' && keyword.length() > 1){
						newtoken = new Token(ASCIISTR,keyword);
						newtoken.setAsciiString("src/" + keyword.substring(1, keyword.length()-1));
						keyword = "";
					}
					//ID
					else if(fileLine.charAt(pos) == ' ' || fileLine.charAt(pos) == ';' || fileLine.charAt(pos) == ')'){
						if(keyword.length() <= 9){
							newtoken = new Token(ID,keyword);
							newtoken.setId(keyword);
							keyword = "";
						}
						else{
							System.out.println("ERROR INVALID ID");
						}
					}	
				}//end if
				if(keyword.equals("") && newtoken != null){
					tokenList.add(newtoken);
					System.out.println(newtoken.getName());
					newtoken = null;
				}
					
					}//end while	
			}//end while
		} catch (FileNotFoundException e) {
			System.err.println("File not found.");
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("And I/O error has occurred");
			e.printStackTrace();
		} finally {
			return tokenList;
		}
	}
	public void generateToken(String keyword){
	
	
		
	}
}
